/*******************************************************************************
 * Copyright (c) 2008, 2009 Tapestry IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Tapestry IDE Developers - initial API and implementation
 *******************************************************************************/
package com.googlecode.tapestry.ide.internal.ui.preferences;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;
import org.osgi.service.prefs.BackingStoreException;

import com.googlecode.tapestry.ide.internal.core.preferences.PreferenceConstants;

/**
 * The Class TapestryProjectPropertyPage.
 */
public class TapestryProjectPropertyPage extends PropertyPage {

	/** The Constant PACKAGE_TITLE. */
	private static final String PACKAGE_TITLE = "Package:";

	/** The Constant TEXT_FIELD_WIDTH. */
	private static final int TEXT_FIELD_WIDTH = 50;

	/** The package text. */
	private Text packageText;

	/** The preferences. */
	private IEclipsePreferences preferences;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public TapestryProjectPropertyPage() {
		super();
	}

	/**
	 * Adds the package section.
	 * 
	 * @param parent
	 *            the parent
	 */
	private void addPackageSection(Composite parent) {
		Composite composite = createDefaultComposite(parent, 2);

		Label infoLabel = new Label(composite, SWT.NONE | SWT.WRAP);
		infoLabel
				.setText("This configuration is to identify the root application package. Tapestry uses this package name to locate your page and component classes.");
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalSpan = 2;
		gd.widthHint = 300;
		infoLabel.setLayoutData(gd);

		Label packageLabel = new Label(composite, SWT.NONE);
		packageLabel.setText(PACKAGE_TITLE);

		packageText = new Text(composite, SWT.SINGLE | SWT.BORDER);
		gd = new GridData();
		gd.widthHint = convertWidthInCharsToPixels(TEXT_FIELD_WIDTH);
		packageText.setLayoutData(gd);

		IProject project = (IProject) ((IAdaptable) getElement())
				.getAdapter(IProject.class);

		IScopeContext context = new ProjectScope(project);
		preferences = context.getNode(PreferenceConstants.QUALIFIER);
		String text = preferences.get(
				PreferenceConstants.APPLICATION_PACKAGE_KEY,
				PreferenceConstants.APPLICATION_PACKAGE_DEFAULT);

		packageText.setText(text);
	}

	/**
	 * Adds the separator.
	 * 
	 * @param parent
	 *            the parent
	 */
	@SuppressWarnings("unused")
	private void addSeparator(Composite parent) {
		Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		separator.setLayoutData(gridData);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse
	 * .swt.widgets.Composite)
	 */
	protected Control createContents(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		composite.setLayout(layout);
		GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		addPackageSection(composite);
		return composite;
	}

	/**
	 * Creates the default composite.
	 * 
	 * @param parent
	 *            the parent
	 * @param numColumns
	 *            the num columns
	 * 
	 * @return the composite
	 */
	private Composite createDefaultComposite(Composite parent, int numColumns) {
		Composite composite = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		composite.setLayout(layout);

		GridData data = new GridData(GridData.FILL_BOTH);
		composite.setLayoutData(data);

		return composite;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
	 */
	protected void performDefaults() {
		packageText.setText(PreferenceConstants.APPLICATION_PACKAGE_DEFAULT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.preference.PreferencePage#performOk()
	 */
	public boolean performOk() {
		preferences.put(PreferenceConstants.APPLICATION_PACKAGE_KEY,
				packageText.getText());
		try {
			preferences.flush();
		} catch (BackingStoreException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

		return true;
	}

}